package com.lc.handler;

import com.lc.constants.HttpConstants;

/**
 * @author liuc
 * @date 2021/5/6
 * @description
 **/
public class ProtocolUtil {
    /**
     * 成功返回
     * @param data 响应数据
     * @return ok
     */
    public static String ok(String data){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(HttpConstants.REQ_HEAD_OK).append(HttpConstants.CRLF)
                .append(HttpConstants.CONTENT_TYPE_TEXT_HTML).append(HttpConstants.CRLF)
                .append(HttpConstants.CONTENT_LENGTH).append(data.getBytes().length)
                .append(HttpConstants.CRLF).append(HttpConstants.ENTER).append(data);
        return stringBuilder.toString();
    }

    /**
     * 404
     * @return 404
     */
    public static String notFound(){
        String notFoundStr = "<h1>404 not found</h1>";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(HttpConstants.REQ_HEAD_OK).append(HttpConstants.CRLF)
                .append(HttpConstants.CONTENT_TYPE_TEXT_HTML).append(HttpConstants.CRLF)
                .append(HttpConstants.CONTENT_LENGTH).append(notFoundStr.getBytes().length)
                .append(HttpConstants.CRLF).append(HttpConstants.ENTER).append(notFoundStr);
        return stringBuilder.toString();
    }

    public static String getHttpHeader200(long contentLength) {
        return "HTTP/1.1 200 OK \n" +
                "Content-Type: text/html \n" +
                "Content-Length: " + contentLength + " \n" +
                "\r\n";
    }

}
